package fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.qianfeng.lingshijia.R;

import java.util.List;

import adapter.DetailsAdapater;
import custom.DetailMidView;
import entity.Details;
import utils.Constants;
import utils.DownloadUtils;

/**
 * 详情页面商品详情
 */
public class DetailsFragment extends BaseFragment {

    private GridView gv;
    private int productId;
    private DetailMidView dmView;
    private DetailsAdapater detailsAdapater;
    private List<Details.DataEntity.GuessLikeListEntity> guessLikeListEntity;
    public void setProductId(int productId){
        this.productId = productId;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_details, container, false);
    }

    @Override
    public void init(View view) {
        super.init(view);
        gv = (GridView) view.findViewById(R.id.id_gv_guess);
        detailsAdapater = new DetailsAdapater(getActivity());
        gv.setAdapter(detailsAdapater);
        dmView = (DetailMidView) view.findViewById(R.id.id_detail_view);

    }

    @Override
    public void loadDatas(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("productId", productId + "");
        DownloadUtils.postJson(Constants.URL.DETAIL_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = responseInfo.result;
                if(response!=null){
                    Gson gson = new Gson();
                    Details details = gson.fromJson(response,Details.class);
                    String sales = null;
                    if(details.getData().getSalesVolume()!=0){
                        sales = details.getData().getSalesVolume()+"";
                    }
                    dmView.setData(details.getData().getImgUrl(),details.getData().getPrice(),
                            sales,details.getData().getTitle());
                    guessLikeListEntity =  details.getData().getGuessLikeList();
                    detailsAdapater.setData(guessLikeListEntity);
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        },params);

    }


}
